


package jnpf.zc.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.exception.DataException;
import jnpf.permission.entity.UserEntity;
import jnpf.util.GeneraterSwapUtil;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import jnpf.zc.entity.ZcglJyghEntity;
import jnpf.zc.model.zcgljygh.ZcglJyghForm;
import jnpf.zc.model.zcgljygh.ZcglJyghInfoVO;
import jnpf.zc.model.zcgljygh.ZcglJyghListVO;
import jnpf.zc.model.zcgljygh.ZcglJyghPagination;
import jnpf.zc.service.ZcglJyghService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;


/**
 *
 * 经营规划
 * @版本： V3.2.0
 * @版权： 智果云科技（https://www.jnpfsoft.com）
 * @作者： JNPF开发平台组
 * @日期： 2024-12-19
 */
@Slf4j
@RestController
@Api(tags = "经营规划" , value = "example")
@RequestMapping("/api/example/ZcglJygh")
public class ZcglJyghController {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;


    @Autowired
    private UserProvider userProvider;


    @Autowired
    private ZcglJyghService zcglJyghService;



    /**
     * 列表
     *
     * @param zcglJyghPagination
     * @return
     */
    @ApiOperation("获取列表")
	@PostMapping("/getList")
    public ActionResult list(@RequestBody ZcglJyghPagination zcglJyghPagination)throws IOException{
        List<ZcglJyghEntity> list= zcglJyghService.getList(zcglJyghPagination);
        //处理id字段转名称，若无需转或者为空可删除

    List<ZcglJyghListVO> listVO=JsonUtil.getJsonToList(list,ZcglJyghListVO.class);
    for(ZcglJyghListVO zcglJyghVO:listVO){
                zcglJyghVO.setPlanningNumber_name(zcglJyghVO.getPlanningNumber());

                zcglJyghVO.setProject_name(zcglJyghVO.getProject());

                zcglJyghVO.setOperatingCompany_name(zcglJyghVO.getOperatingCompany());

                zcglJyghVO.setModeOperation_name(zcglJyghVO.getModeOperation());

                zcglJyghVO.setBusinessStatus_name(zcglJyghVO.getBusinessStatus());

                zcglJyghVO.setCreatortime_name(zcglJyghVO.getCreatortime());

                zcglJyghVO.setNotes_name(zcglJyghVO.getNotes());

		//子表数据转换
     }

         PageListVO vo=new PageListVO();
         vo.setList(listVO);
         PaginationVO page=JsonUtil.getJsonToBean(zcglJyghPagination,PaginationVO.class);
         vo.setPagination(page);
         return ActionResult.success(vo);

    }

    /**
     * 创建
     *
     * @param zcglJyghForm
     * @return
     */
    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid ZcglJyghForm zcglJyghForm) throws DataException {
        boolean b =  zcglJyghService.checkForm(zcglJyghForm,0);
        if (b){
         return ActionResult.fail("单行输入不能重复");
        }
        String mainId =RandomUtil.uuId();

        UserInfo userInfo=userProvider.get();
		UserEntity userEntity = generaterSwapUtil.getUser(userInfo.getUserId());
        ZcglJyghEntity entity = JsonUtil.getJsonToBean(zcglJyghForm, ZcglJyghEntity.class);
            entity.setId(mainId);
                zcglJyghService.save(entity);


        return ActionResult.success("创建成功");
}



    /**
    * 信息
    *
    * @param id
    * @return
    */
    @ApiOperation("信息")
    @GetMapping("/{id}")
    public ActionResult<ZcglJyghInfoVO> info(@PathVariable("id") String id){
        ZcglJyghEntity entity= zcglJyghService.getInfo(id);
        ZcglJyghInfoVO vo=JsonUtil.getJsonToBean(entity, ZcglJyghInfoVO.class);

    //子表
    //副表
        return ActionResult.success(vo);
    }

    /**
    * 表单信息(详情页)
    *
    * @param id
    * @return
    */
    @ApiOperation("表单信息(详情页)")
    @GetMapping("/detail/{id}")
    public ActionResult<ZcglJyghInfoVO> detailInfo(@PathVariable("id") String id){
        ZcglJyghEntity entity= zcglJyghService.getInfo(id);
        ZcglJyghInfoVO vo=JsonUtil.getJsonToBean(entity, ZcglJyghInfoVO.class);

 //子表数据转换

 //附表数据转换

//添加到详情表单对象中
                vo.setPlanningNumber(vo.getPlanningNumber());

                vo.setProject(vo.getProject());

                vo.setOperatingCompany(vo.getOperatingCompany());

                vo.setModeOperation(vo.getModeOperation());

                vo.setBusinessStatus(vo.getBusinessStatus());

                vo.setCreatortime(vo.getCreatortime());

                vo.setNotes(vo.getNotes());


        return ActionResult.success(vo);
		}




   /**
    * 更新
    *
    * @param id
    * @param zcglJyghForm
    * @return
    */
    @PutMapping("/{id}")
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id,@RequestBody @Valid ZcglJyghForm zcglJyghForm) throws DataException {
        boolean b =  zcglJyghService.checkForm(zcglJyghForm,1);
        if (b){
         return ActionResult.fail("单行输入不能重复");
        }
        UserInfo userInfo=userProvider.get();
        ZcglJyghEntity entity= zcglJyghService.getInfo(id);
        if(entity!=null){
            ZcglJyghEntity subentity=JsonUtil.getJsonToBean(zcglJyghForm, ZcglJyghEntity.class);
				boolean b1 = zcglJyghService.updateById(subentity);
				if (!b1){
				return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
				}
        return ActionResult.success("更新成功");
        }else{
            return ActionResult.fail("更新失败，数据不存在");
        }
    }



   /**
    * 删除
    *
    * @param id
    * @return
    */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id){
        ZcglJyghEntity entity= zcglJyghService.getInfo(id);
        if(entity!=null){
            zcglJyghService.delete(entity);

        }
        return ActionResult.success("删除成功");
    }






}
